#include<iostream>
#include <unordered_map>
#include <vector>

using namespace std;

class Solution {
public:
    int maxLength(vector<int>& arr) {
        unordered_map<int, int> m;
        int n = arr.size(), cnt = 0;
        int l = 0;
        int res = 0;
        for (int r = 0; r < n; r++)
        {
            m[arr[r]]++;
            if (m[arr[r]] > 1)   cnt++;

            while (cnt != 0)
            {
                m[arr[l]]--;
                if (m[arr[l]] == 1)  cnt--;
                l++;
            }
            res = max(res, r - l + 1);
        }
        return res;
    }
};